package com.ricchieri.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.dao.interf.IScheduleCharterDao;
import com.ricchieri.filter.ScheduleCharterFilter;
import com.ricchieri.model.ScheduleCharter;
import com.ricchieri.util.DateUtil;
import com.ricchieri.util.StringUtils;

public class ScheduleCharterDao extends GenericDao implements IScheduleCharterDao {
	private static final long serialVersionUID = -4035843977481120016L;

	@Override
	@SuppressWarnings("unchecked")
	public List<ScheduleCharter> getAllScheduleCharter(ScheduleCharterFilter filter) {
		Criteria criteria = criteriaAllScheduleCharters(filter);
		
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ScheduleCharter> getAllScheduleCharterPaginatedByFilter(
			ScheduleCharterFilter charterFilter) {
		Criteria criteria = criteriaAllScheduleCharters(charterFilter);
		
		return criteria.list();
	}
	
	private Criteria criteriaAllScheduleCharters(ScheduleCharterFilter scheduleCharter) {
		Criteria criteria = super.criteriaAllBasic(scheduleCharter, ScheduleCharter.class);
		

		if (!StringUtils.isEmpty(scheduleCharter.getTrip())) {
			criteria.createAlias(getTableName().concat(".trip"), "trip");
			criteria.add(Restrictions.eq("trip.id", scheduleCharter.getTripId()));
		}

		if (scheduleCharter.getUserId() > 0) {
			criteria.createAlias(getTableName().concat(".user"), "user");
			criteria.add(Restrictions.eq("user.id", scheduleCharter.getUserId()));
		}

		if (scheduleCharter.getStart() != null && scheduleCharter.getFinish() != null) {
			criteria.createAlias(getTableName().concat(".schedule"), "schedule");
			
			criteria.add(Restrictions.ge("schedule.date", DateUtil.changeToStartDay(scheduleCharter.getStart())));
			criteria.add(Restrictions.le("schedule.date", DateUtil.changeToEndDay(scheduleCharter.getFinish())));
		}

		return criteria;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ScheduleCharter> getAllScheduleCharterPaginatedByFilter(
			ScheduleCharterFilter charterFilter, int startRecord) {
		Criteria criteria = criteriaAllScheduleCharters(charterFilter);
		criteria.setMaxResults(BusinessSettings.ROW_MAX_RESULT);
		criteria.setFirstResult(startRecord);
		return criteria.list();
	}

	@Override
	public int getAllScheduleCharterCountByFilter(
			ScheduleCharterFilter charterFilter) {
		Criteria criteria = criteriaAllScheduleCharters(charterFilter);
		Long result = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
		return new Integer(result.intValue());
	}

	@Override
	@SuppressWarnings("unchecked")
	public ScheduleCharter getScheduleCharterByFilter(
			ScheduleCharterFilter filter) {
		Criteria criteria = criteriaAllScheduleCharters(filter);
		List<ScheduleCharter> scheduleCharters = criteria.list();
		
		if(scheduleCharters == null) {
			return null;
		} else {
			return scheduleCharters.get(0);
		}
	}

	@Override
	protected String getTableName() {
		return "scheduleCharter";
	}
}
